home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / comms / internet / html-related / hsc / source / hscprj / readprj.c < prev    next >
C/C++ Source or Header  |  1996-09-12  |  12KB  |  446 lines

  1. /*
  2.  * hscprj/readprj.c
  3.  *
  4.  * project managment input-routines for hsc
  5.  *
  6.  * Copyright (C) 1995,96  Thomas Aglassinger
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to the Free Software
  20.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  *
  22.  * updated: 10-Sep-1996
  23.  * created: 10-Sep-1996
  24.  */
  25.  
  26. #include <stdio.h>
  27. #include <stdarg.h>
  28. #include <string.h>
  29. #include <errno.h>
  30. #include <time.h>
  31.  
  32. #include "hsclib/ldebug.h"
  33. #include "hscprj/pdebug.h"
  34. #include "hscprj/pdefs.h"
  35.  
  36. #include "ugly/utypes.h"
  37. #include "ugly/dllist.h"
  38. #include "ugly/expstr.h"
  39. #include "ugly/umemory.h"
  40. #include "ugly/infile.h"
  41. #include "ugly/ustring.h"
  42.  
  43. #include "hscprj/document.h"
  44. #include "hscprj/project.h"
  45.  
  46. /*
  47.  * hsc_msg_project_corrupt: display warning about
  48.  * corrupt project file
  49.  */
  50. static VOID hsc_msg_project_corrupt(HSCPRJ * hp, STRPTR descr)
  51. {
  52.     hp->fatal = TRUE;
  53.     if (hp->CB_msg_corrupt_pf)
  54.         (*(hp->CB_msg_corrupt_pf)) (hp, descr);
  55. }
  56.  
  57. /* convert hex-digit to int */
  58. static int x2int(char c)
  59. {
  60.     if ((c >= '0') && (c <= '9'))
  61.         return (c - '0');
  62.     if ((c >= 'A') && (c <= 'F'))
  63.         return (c - 'A' + 10);
  64.     if ((c >= 'a') && (c <= 'f'))
  65.         return (c - 'a' + 10);
  66.     return (EOF);
  67. }
  68.  
  69. /*
  70.  * read_long
  71.  */
  72. static ULONG read_ulong(HSCPRJ * hp)
  73. {
  74.     INFILE *inpf = hp->inpf;
  75.     ULONG num = 0;
  76.     int ch;
  77.     int digit = EOF;
  78.  
  79.     do
  80.     {
  81.         ch = infgetc(inpf);
  82.         if (ch != ' ')
  83.         {
  84.             digit = x2int(ch);
  85.             if (digit == EOF)
  86.                 num = 0;
  87.             else
  88.                 num = (num << 4) + digit;
  89.         }
  90.         if (digit == EOF)
  91.             hsc_msg_project_corrupt(hp, "hex digit expected");
  92.     }
  93.     while ((digit != EOF) && (ch != ' '));
  94.  
  95.     if (digit == EOF)
  96.         num = 0;
  97.  
  98.     return (num);
  99. }
  100.  
  101. /*
  102.  * read_string:
  103.  *
  104.  * read string length, alloc mem and read string into it
  105.  */
  106. static STRPTR read_string(HSCPRJ * hp)
  107. {
  108.     STRPTR dest = NULL;
  109.  
  110.     ULONG len = read_ulong(hp);
  111.  
  112.     if (len)
  113.     {
  114.         ULONG i;
  115.         int ch = 'x';           /* dummy init */
  116.  
  117.         /* alloc mem */
  118.         dest = umalloc((size_t) (len + 1));
  119.         dest[len] = '\0';
  120.  
  121.         for (i = 0; ((i < len) && (ch != EOF)); i++)
  122.         {
  123.             ch = infgetc(hp->inpf);
  124.             if (ch != EOF)
  125.                 dest[i] = ch;
  126.             else
  127.                 hsc_msg_project_corrupt(hp, "string expected");
  128.         }
  129.         if (ch != EOF)
  130.             dest[len] = 0;
  131.         else
  132.         {
  133.             ufree(dest);
  134.             dest = NULL;
  135.         }
  136.     }
  137.     return (dest);
  138. }
  139.  
  140. /*
  141.  * read_caller: read file position
  142.  */
  143. static CALLER *read_caller(HSCPRJ * hp)
  144. {
  145.     CALLER *caller = NULL;
  146.     STRPTR callerid = infgetw(hp->inpf);
  147.  
  148.     if (callerid && !upstrcmp(callerid, ID_CALLER_STR))
  149.     {
  150.         int ch = infgetc(hp->inpf);     /* skip blank */
  151.  
  152.         if (ch == ' ')
  153.         {
  154.             STRPTR fname = read_string(hp);
  155.             caller = new_caller(fname, read_ulong(hp), read_ulong(hp));
  156.             ufreestr(fname);
  157.         }
  158.         else
  159.         {
  160.             hsc_msg_project_corrupt(hp, "blank expected");
  161.         }
  162.     }
  163.     else if (callerid && !strcmp(callerid, "\n"))
  164.     {
  165.         /* skip empty caller */
  166.         inungetcw(hp->inpf);
  167.         D(fprintf(stderr, DHP "skip EMPTY CALLER\n"));
  168.     }
  169.     else
  170.     {
  171.         hsc_msg_project_corrupt(hp, ID_CALLER_STR " expected");
  172.     }
  173.     return (caller);
  174. }
  175.  
  176. /*
  177.  * read_lf: read linefeed
  178.  */
  179. static BOOL read_lf(HSCPRJ * hp)
  180. {
  181.     int ch = infgetc(hp->inpf);
  182.     BOOL ok = TRUE;
  183.  
  184.     if (ch != '\n')
  185.     {
  186.         hsc_msg_project_corrupt(hp, "linefeed expected");
  187.         ok = FALSE;
  188.     }
  189.     return (ok);
  190. }
  191.  
  192. /*
  193.  * read command
  194.  *
  195.  * returns next command or NULL if eof reached
  196.  */
  197. static STRPTR read_command(HSCPRJ * hp)
  198. {
  199.     STRPTR command;
  200.  
  201.     do
  202.     {
  203.         command = infgetw(hp->inpf);
  204.     }
  205.     while (command && !strcmp(command, "\n"));
  206.  
  207.     if (command)
  208.     {
  209.         /* skip blanks */
  210.         infskip_ws(hp->inpf);
  211.         DP(fprintf(stderr, DHP "command `%s'\n", command));
  212.     }
  213.     else
  214.     {
  215.         DP(fprintf(stderr, DHP "command EOF\n"));
  216.     }
  217.  
  218.     return (command);
  219. }
  220.  
  221. /*
  222.  * read header
  223.  */
  224. static BOOL read_header(HSCPRJ * hp)
  225. {
  226.     STRARR fileid[1 + sizeof(FILEID_HSCPRJ)];
  227.     BOOL ok = FALSE;
  228.     STRPTR cmd = NULL;
  229.     size_t i;
  230.  
  231.     /* read fileid */
  232.     for (i = 0; i < strlen(FILEID_HSCPRJ); i++)
  233.     {
  234.         int ch = infgetc(hp->inpf);
  235.  
  236.         fileid[i] = (UBYTE) ch;
  237.     }
  238.     fileid[i] = '\0';
  239.  
  240.     DP(fprintf(stderr, DHP "fileid: `%s'\n", fileid));
  241.  
  242.     /* check fileid */
  243.     if (!strcmp(fileid, FILEID_HSCPRJ))
  244.     {
  245.         DP(fprintf(stderr, DHP "fileid: `%s'\n", fileid));
  246.         ok = read_lf(hp);
  247.     }
  248.     else
  249.     {
  250.         hsc_msg_project_corrupt(hp, "wrong file-id");
  251.     }
  252.  
  253.     if (ok)
  254.     {
  255.  
  256.         ok = FALSE;
  257.  
  258.         /* read version */
  259.         cmd = read_command(hp);
  260.  
  261.         /* check version */
  262.         if (cmd && !strcmp(cmd, LINE_VERSION_STR))
  263.         {
  264.             ULONG version = read_ulong(hp);
  265.  
  266.             DP(fprintf(stderr, DHP "version: %lu\n", version));
  267.  
  268.             if (version && (version <= 2))
  269.             {
  270.                 ok = read_lf(hp);
  271.             }
  272.             else
  273.             {
  274.                 hsc_msg_project_corrupt(hp, "wrong version");
  275.             }
  276.  
  277.         }
  278.         else
  279.         {
  280.             hsc_msg_project_corrupt(hp, "unknown version");
  281.         }
  282.  
  283.     }
  284.     return (ok);
  285. }
  286.  
  287. /*
  288.  * hsc_project_read_file
  289.  *
  290.  * read project file
  291.  *
  292.  * params: hp....HSCPRJ created using new_project()
  293.  *         inpf..input file opened using infopen();
  294.  *               this has to be closed by you.
  295.  */
  296. BOOL hsc_project_read_file(HSCPRJ * hp, INFILE * inpf)
  297. {
  298.     BOOL ok = FALSE;
  299.  
  300.     if (inpf)
  301.     {
  302.         hp->inpf = inpf;
  303.  
  304.         DP(fprintf(stderr, DHP "read project-file from `%s'\n",
  305.                      infget_fname(inpf)));
  306.  
  307.         if (read_header(hp))
  308.         {
  309.             HSCDOC *document = NULL;
  310.             STRPTR cmd = NULL;
  311.  
  312.             do
  313.             {
  314.                 cmd = read_command(hp);
  315.                 if (cmd)
  316.                 {
  317.                     if (!strcmp(cmd, LINE_REM_STR))
  318.                     {
  319.                         /* skip comment */
  320.                         int ch;
  321.                         DP(fprintf(stderr, DHP "comment `"));
  322.                         do
  323.                         {
  324.                             ch = infgetc(inpf);
  325.                             DP(
  326.                                     {
  327.                                     if (ch != '\n')
  328.                                     fprintf(stderr, "%c", ch);
  329.                                     }
  330.                             );
  331.                         }
  332.                         while ((ch != EOF) && (ch != '\n'));
  333.                         DP(fprintf(stderr, "'\n"));
  334.                     }
  335.                     else if (!strcmp(cmd, LINE_DOCUMENT_STR))
  336.                     {
  337.                         /* begin new DOCUMENT */
  338.                         STRPTR docname = read_string(hp);
  339.                         if (docname)
  340.                         {
  341.                             document = new_document(docname);
  342.                             app_dlnode(hp->documents, (APTR) document);
  343.                             /* free mem allocated by read_string() */
  344.                             ufree(docname);
  345.                         }
  346.                     }
  347.                     else if (!strcmp(cmd, LINE_SOURCE_STR))
  348.                     {
  349.                         /* assign SOURCE */
  350.                         if (document)
  351.                         {
  352.                             STRPTR sourcename = read_string(hp);
  353.                             if (sourcename)
  354.                             {
  355.                                 reallocstr(&(document->sourcename),
  356.                                            sourcename);
  357.                                 /* free mem allocated by read_string() */
  358.                                 ufree(sourcename);
  359.                             }
  360.                         }
  361.                         else
  362.                             hsc_msg_project_corrupt
  363.                                 (hp, LINE_SOURCE_STR " without "
  364.                                  LINE_DOCUMENT_STR);
  365.                     }
  366.                     else if (!strcmp(cmd, LINE_TITLE_STR))
  367.                     {
  368.                         /* assign TITLE */
  369.                         if (document)
  370.                         {
  371.                             STRPTR titlename = read_string(hp);
  372.                             if (titlename)
  373.                             {
  374.                                 set_estr(document->title, titlename);
  375.                                 /* free mem allocated by read_string() */
  376.                                 ufree(titlename);
  377.                             }
  378.                         }
  379.                         else
  380.                             hsc_msg_project_corrupt
  381.                                 (hp, LINE_TITLE_STR " without "
  382.                                  LINE_DOCUMENT_STR);
  383.                     }
  384.                     else if (!strcmp(cmd, LINE_ID_STR))
  385.                     {
  386.                         /* append new ID */
  387.                         if (document)
  388.                         {
  389.                             STRPTR idname = read_string(hp);
  390.                             if (idname)
  391.                             {
  392.                                 HSCIDD *iddef =
  393.                                 app_iddef(document, idname);
  394.                                 iddef->caller = read_caller(hp);
  395.                                 /* free mem allocated by read_string() */
  396.                                 ufree(idname);
  397.                             }
  398.                         }
  399.                         else
  400.                             hsc_msg_project_corrupt
  401.                                 (hp, LINE_ID_STR " without "
  402.                                  LINE_DOCUMENT_STR);
  403.  
  404.                     }
  405.                     else if (!strcmp(cmd, LINE_INCLUDE_STR))
  406.                     {
  407.                         /* store new INCLUDE */
  408.                         if (document)
  409.                         {
  410.                             STRPTR incname = read_string(hp);
  411.                             if (incname)
  412.                             {
  413.                                 HSCINC *inc =
  414.                                 app_include(document, incname);
  415.                                 inc->caller = read_caller(hp);
  416.                                 /* free mem allocated by read_string() */
  417.                                 ufree(incname);
  418.                             }
  419.                         }
  420.                         else
  421.                             hsc_msg_project_corrupt
  422.                                 (hp, LINE_INCLUDE_STR " without "
  423.                                  LINE_DOCUMENT_STR);
  424.                     }
  425.                     else
  426.                     {
  427.                         /* unknown command */
  428.                         hsc_msg_project_corrupt(hp, "unknown tag");
  429.                     }
  430.                 }
  431.                 else
  432.                 {
  433.                     DP(fprintf(stderr, DHP "EOF\n"));
  434.                 }
  435.             }
  436.             while (cmd && !hp->fatal);
  437.  
  438.             ok = !hp->fatal;
  439.         }
  440.         hp->inpf = NULL;
  441.     }
  442.  
  443.     return (ok);
  444. }
  445.  
  446.